import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login/Login.vue'
import Home from '@/views/Home/Home.vue'
import Welcome from '@/components/Welcome/Welcome.vue'
import Users from '@/views/Users/Users.vue'
import Rights from '@/views/Rights/Rights.vue'
import Roles from '@/views/Roles/Roles.vue'
import Categories from '@/views/Categories/Categories.vue'
import GoodsParams from '@/views/GoodsParams/GoodsParams.vue'
import GoodsList from '@/views/GoodsList/GoodsList.vue'
import Addgoods from '@/views/AddGoods/Addgoods.vue'
import Orders from '@/views/Orders/Orders.vue'
import Report from '@/views/Report/Report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/home/Welcome',
    children: [
      { path: 'Welcome', component: Welcome },
      { path: 'users', component: Users },
      { path: 'rights', component: Rights },
      { path: 'Roles', component: Roles },
      { path: 'categories', component: Categories },
      { path: 'params', component: GoodsParams },
      { path: 'goods', component: GoodsList },
      { path: 'add', name: 'add', component: Addgoods },
      { path: 'orders', component: Orders },
      { path: 'reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由前置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 未登录就强制跳转到登录页
  if (!tokenStr) return next('/login')
  // token有值就直接放行
  next()
})

export default router
